/**
 * CorpRiskInfoComponentImpl.java
 * author: yujiakui
 * 2017年8月29日
 * 下午3:39:29
 */
package com.ctim.service.component.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import java.text.MessageFormat;

import org.springframework.stereotype.Component;

import com.ctim.common.exception.PcsRunTimeException;
import com.ctim.model.IsvRequestParamInfo;
import com.ctim.model.ThirdPartyInvokeCfg;
import com.ctim.service.enums.ParserTypeEnum;
import com.ctim.service.enums.ValidatorTypeEnum;
import com.ctim.service.integration.ThirdPartyResponseHandleTemplate;
import com.ctim.service.integration.parser.ThirdPartyParserConfig;

/**
 * @author yujiakui
 *
 *         下午3:39:29
 *
 *         个人征信采集组件实现
 */
@Component("personCreditInfoCollectionComponent")
public class PersonCreditInfoCollectionComponent extends AbstractCreditInfoCollectionComponent {

	/*
	 *
	 * @see
	 * com.ctim.service.component.impl.AbstractCreditInfoCollectionComponent#
	 * assembleHandleTemplate(com.ctim.model.ThirdPartyInvokeCfg,
	 * com.ctim.model.IsvRequestParamInfo)
	 */
	@Override
	protected ThirdPartyResponseHandleTemplate assembleHandleTemplate(ThirdPartyInvokeCfg invokeCfg,
			IsvRequestParamInfo isvRequestParamInfo) {
		return new ThirdPartyResponseHandleTemplate() {

			@Override
			protected ValidatorTypeEnum getValidateType() {
				// TODO Auto-generated method stub
				return ValidatorTypeEnum.STRING_VALIDATOR;
			}

			@Override
			protected ParserTypeEnum getParserType() {
				// TODO Auto-generated method stub
				return ParserTypeEnum.STRING_XML;
			}

			@Override
			protected ThirdPartyParserConfig getParserConfig() {

				File cfgFile = new File(isvRequestParamInfo.getReqParserMappingUrl());
				if (!cfgFile.exists()) {
					throw new PcsRunTimeException(MessageFormat.format("路径={0}对应的解析映射配置文件不存在{1}",
							isvRequestParamInfo.getReqParserMappingUrl(), isvRequestParamInfo));

				}
				Reader cfgReader;
				try {
					cfgReader = new FileReader(cfgFile);
				} catch (FileNotFoundException e) {
					throw new PcsRunTimeException(MessageFormat.format("路径={0}对应的解析映射配置文件不存在{1}",
							isvRequestParamInfo.getReqParserMappingUrl(), isvRequestParamInfo));

				}
				ThirdPartyParserConfig parserConfig = new ThirdPartyParserConfig();
				parserConfig.setMappingCfgReader(cfgReader);
				return parserConfig;
			}
		};
	}

}
